#include<iostream>
#include<deque>
#include<cstring>
using namespace std;

const int N = 550;
int sign[N][N];
char arr[N][N];
int n, m, w1, v1, x2, y2;
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };

int dfs()
{
	memset(sign, -1, sizeof sign);
	deque<pair<int, int> > q;
	q.push_front({ w1, v1 });
	sign[w1][v1] = 0;
	while (q.size())
	{
		auto ch = q.front(); q.pop_front();
		int x = ch.first, y = ch.second;
		if (x == x2 && y == y2) return sign[x][y];
		for (int k = 0; k < 4; k++)
		{
			int i = x + dx[k], j = y + dy[k];
			if (i<0 || j<0 || i>=n || j>=m) continue;
			int num = arr[i][j] != arr[x][y] ? 1 : 0;
			if (sign[i][j] == -1)
			{
				sign[i][j] = sign[x][y]+num;
				if (sign[i][j] == 0) q.push_front({ i, j });
				else q.push_back({ i, j });
			}
			else if (sign[i][j] > sign[x][y]+ num) sign[i][j] = sign[x][y] + num;
		}
	}
}
int main()
{
	while (cin >> n >> m, n  m)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				cin >> arr[i][j];
			}
		}
		cin >> w1 >> v1 >> x2 >> y2;
		cout << dfs() << endl;
	}
	return 0;
}
